﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;

namespace GXRTBTC.DataReceiveService
{
    class Program
    {
        static Task Main(string[] args) =>
            CreateHostBuilder(args).Build().RunAsync();

        static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(config =>
                {
                    string rootPath = "";

#if DEBUG
                    //本地，使用公共配置文件，当前路径往上四层
                    rootPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.Parent.ToString() + "\\PublicClassLibrary\\";
#endif

                    //挂载的配置文件
                    //挂载的出现过问题，这里做个判断
                    if (File.Exists(rootPath + "common.json"))
                    {
                        //添加配置文件,热更新
                        config.AddJsonFile(rootPath + "common.json", false, true);
                        Console.WriteLine("挂载配置文件common.json");
                    }
                    //自带的配置文件
                    else
                    {
                        //添加配置文件,热更新
                        config.AddJsonFile(rootPath + "common_local.json", false, true);
                        Console.WriteLine("自带配置文件common_local.json");
                    }
                })
                .ConfigureLogging(logging => //配置日志，使用Nlog
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                    logging.AddNLog(new NLogProviderOptions
                    {
                        CaptureMessageProperties = true,
                        CaptureMessageTemplates = true
                    });
                })
                .ConfigureServices((hostContext, services) =>
                {
                    //添加后台运行服务
                    services.AddHostedService<Worker>();

                })
;
    }
}

